package alpaca.view.panels;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;

import net.miginfocom.swing.MigLayout;
import alpaca.core.IUser;
import alpaca.core.UserList;
import alpaca.ctrl.ControllerFactory;
import alpaca.ctrl.RemoveUserCtrl;

public class UserPanel extends JPanel {
	private DefaultListModel listModel;
	private JList list;
	private final Action action = new AddAction();
	private final Action action_1 = new EditAction();
	private final Action action_2 = new RemoveAction();

	/**
	 * Create the panel.
	 */
	 public UserPanel() {
		 listModel = new DefaultListModel();
		 setLayout(new MigLayout("", "[398px,grow]", "[464px,grow][]"));

		 JPanel panel_1 = new JPanel();
		 panel_1.setBorder(new TitledBorder(null, "Edit Users", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		 add(panel_1, "cell 0 0,grow");
		 panel_1.setLayout(new MigLayout("", "[300px,grow][40px]", "[150px][grow]"));

		 JScrollPane scrollPane = new JScrollPane();
		 panel_1.add(scrollPane, "cell 0 0 1 2,grow");

		 list = new JList();
		 scrollPane.setViewportView(list);
		 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		 list.setModel(listModel);

		 JPanel panel = new JPanel();
		 panel_1.add(panel, "flowx,cell 1 0,grow");
		 panel.setLayout(new MigLayout("", "[]", "[grow][grow][grow]"));

		 JButton btnNewButton = new JButton("New button");
		 btnNewButton.setAction(action);
		 panel.add(btnNewButton, "cell 0 0,growx,aligny center");

		 JButton btnNewButton_1 = new JButton("New button");
		 btnNewButton_1.setAction(action_1);
		 panel.add(btnNewButton_1, "cell 0 1,growx");

		 JButton btnNewButton_2 = new JButton("New button");
		 btnNewButton_2.setAction(action_2);
		 panel.add(btnNewButton_2, "cell 0 2,growx");

		 btnNewButton.setToolTipText("Click to add a new user, another window will be shown");
		 btnNewButton_1.setToolTipText("Click to edit the selected user, another window will be shown");
		 btnNewButton_2.setToolTipText("Click to remove the selected user, you'll have to confirm it");

		 btnNewButton.setMnemonic(KeyEvent.VK_A);
		 btnNewButton_1.setMnemonic(KeyEvent.VK_D);
		 btnNewButton_2.setMnemonic(KeyEvent.VK_R);

		 btnNewButton.setDisplayedMnemonicIndex(0);
		 btnNewButton_1.setDisplayedMnemonicIndex(1);
		 btnNewButton_2.setDisplayedMnemonicIndex(0);
	 }

	 public void fillUserList(UserList users) {
		 listModel.clear();
		 for(IUser user : users) {
			 listModel.addElement(user);
		 }   
	 }

	 public void clearUserList(){
		 listModel.clear();
	 }

	 private class AddAction extends AbstractAction {
		 public AddAction() {
			 putValue(NAME, "Add");

		 }
		 @Override
		public void actionPerformed(ActionEvent e) {
			 new UserAddDialog();
		 }
	 }
	 private class EditAction extends AbstractAction {
		 public EditAction() {
			 putValue(NAME, "Edit");

		 }
		 @Override
		public void actionPerformed(ActionEvent e) {
			 if (list.getSelectedValue() != null){
				 new UserEditDialog((IUser) list.getSelectedValue());
			 }
		 }
	 }
	 private class RemoveAction extends AbstractAction {
		 public RemoveAction() {
			 putValue(NAME, "Remove");
		 }
		 @Override
		public void actionPerformed(ActionEvent e) {
			 if (list.getSelectedValue() != null){
				 IUser user = (IUser) list.getSelectedValue();
				 int response = JOptionPane.showConfirmDialog(null, "Do you really wish to remove " + user.toString() + "?", "Remove User", JOptionPane.YES_NO_OPTION);
				 if (response == JOptionPane.YES_OPTION){
					 RemoveUserCtrl ctrl = (RemoveUserCtrl) ControllerFactory.getController(RemoveUserCtrl.class);
					 ctrl.setUser(user);
					 ctrl.execute();
				 }
			 }
		 }
	 }
}